<template>
        <div class="mb20">
            <div class="pub-table-action-btn" v-if="!disabled">
                <a-button icon="plus" class="btn_right" type="primary" @click="handleAddEditPost('', 1)"
                    >新增岗位</a-button
                >
            </div>
            <a-table :pagination="pagination" :columns="columns" :data-source="tableData" row-key="id">
                <template slot="operation" slot-scope="text, record">
                    <div class="pub-table-list-action" v-if="!disabled">
                        <span @click="() => handleAddEditPost(record, 2)">编辑</span>
                        <span @click="() => handleAddEditJob(record)">关联下属岗位</span>
                        <span @click="() => handleDeleteWarning(record)">删除</span>
                    </div>
                    <div class="pub-table-list-action" v-else>
                        <span @click="() => handleAddEditJob(record)">关联下属岗位</span>
                    </div>
                </template>
            </a-table>
            <addEditPost
                :visible="visible"
                :postIndex="postIndex"
                :postInfo="postInfo"
                @handleCancel="handleCancel"
                @getTable="getTable"
            ></addEditPost>

            <addEditJob
                :visibleJob="visibleJob"
                :postInfo="postInfo"
                @handleCancel="handleCancel"
                @getTable="getTable"
            ></addEditJob>
        </div>
</template>

<script>
import { columns, baseColumn } from './model'
import { getPostManageList, deletePostManageList } from '@/api/system/system'
import addEditPost from './addEditPost'

import addEditJob from './addEditJob'
export default {
    name: 'PostManage',
    components: {
        addEditPost,
        addEditJob,
    },
    props: {
        orgId: {
            type: String,
            default: ''
        },
        disabled: {
            type: Boolean,
            default: false
        }
    },
    data() {
        return {
            visible: false,
            visibleJob: false,
            postIndex: 1,
            postInfo: {},

            columns: columns,
            chooseData: 0,
            tableData: [],

            selectedRowKeys: [],
            selectedRows: [],
            selectOptions: {}, // 选中节点对象

            pagination: {
                defaultCurrent: 1, // 默认当前页数
                defaultPageSize: 10, // 默认当前页显示数据的大小
                total: 0, // 总数，必须先有
                showSizeChanger: true,
                showQuickJumper: true,
                pageSizeOptions: ['5', '10', '15', '20'],
                showTotal: (total) => `共 ${total} 条`, // 显示总数
                onShowSizeChange: (current, pageSize) => {
                    this.pagination.defaultCurrent = 1
                    this.pagination.defaultPageSize = pageSize
                    this.getTable() //显示列表的接口名称
                },
                // 改变每页数量时更新显示
                onChange: (current, size) => {
                    this.pagination.defaultCurrent = current
                    this.pagination.defaultPageSize = size
                    this.getTable()
                },
            },
        }
    },
    created() {
        // let params = this.$route.params
        // console.log(params)
        // this.orgId = params.orgId
        this.initPage()
        this.getTable()
    },
    mounted() {},
    methods: {
        initPage() {
            const isCompany = this.$route.query.isCompany
            if(!isCompany) {
                this.columns = baseColumn
            }
        },
        /**
         *@desc  获取组织列表
         *@param {}
         */
        getTable() {
            let that = this
            console.log(that.orgId)
            if (that.orgId) {
                // 获取列表
                that.handleTable()
            }
        },
        /**
         *@desc  table表格列表
         *@param {}
         */
        handleTable() {
            if(!this.orgId) return
            let _this = this
            let params = {
                resultWithEmployee: true, // 为true时返回岗位人员
                pageNumber: _this.pagination.defaultCurrent,
                pageSize: _this.pagination.defaultPageSize,
                orgId: _this.orgId,
            }
            getPostManageList(params).then((res) => {
                if (res.status === 200) {
                    let data = res.data
                    _this.tableData = data.map((val) => {
                        if (val.isLeader === 1) {
                            val.isLeader = '是'
                        } else if (val.isLeader === 0) {
                            val.isLeader = '否'
                        }
                        return val
                    })
                }
            })
        },
        /**
         *@desc  table表格选中
         *@param {}
         */
        onSelectChange(selectedRowKeys, selectedRows) {
            let _this = this
            _this.selectId = []
            selectedRows.map((item) => {
                // 选中id
                _this.selectId.push(item.id)
            })
            this.chooseData = this.chooseData + selectedRows.length
            _this.selectedRowKeys = selectedRowKeys
            _this.selectedRows = selectedRows
        },
        /**
         *@desc  table表格搜索 暂停查询
         *@param {}
         */
        /**
         *@desc  table新增编辑
         *@param {}
         */
        handleAddEditPost(record, index) {
            this.postIndex = index
            console.log(this.orgId)

            this.postInfo = record == '' ? { orgId: this.orgId } : record
            console.log('postInfo')
            console.log(this.postInfo)
            this.visible = true
        },
        /**
         *@desc  添加关联岗位
         *@param {}
         */
        handleAddEditJob(record) {
            console.log(record)
            this.postInfo = record

            this.visibleJob = true
            this.postInfo = record
        },
        /**
         *@desc  table表格删除
         *@param {}
         */
        handleDeleteWarning(record) {
            let _this = this
            _this.$confirm({
                title: `你确定要删除${record.name}吗？`,
                okText: '确认',
                okType: 'danger',
                cancelText: '取消',
                onOk() {
                    _this.handleDelete(record)
                },
                onCancel() {
                    // console.log('Cancel');
                },
            })
        },
        handleDelete(record) {
            deletePostManageList({ id: record.id }).then((res) => {
                if (res.status === 200) {
                    //删除成功
                    this.getTable()
                }
            })
        },
        handleCancel() {
            this.visible = false
            this.visibleJob = false
            this.getTable()
        },
    },
}
</script>

<style scoped>
</style>
